package com.ysz.globalexception.exception;

import com.ysz.controller.TestException;
import com.ysz.globalexception.components.BaseException;
import com.ysz.globalexception.components.BusinessException;
import com.ysz.globalexception.retresult.ReturnResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;


/**
 * @ClassName GlobalException
 * @Description TODO
 * @Data 13:15
 * @Version 1.0
 * @Author ysz
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ReturnResult exception(Exception e) {
        return ReturnResult.builder().code(3024).data(null).msg("暂无内容").build();
    }


    @ExceptionHandler(BaseException.class)
    public ReturnResult baseException(BaseException e) {
        return ReturnResult.builder().code(e.getCode()).data(null).msg(e.getMsg()).build();
    }


    @ExceptionHandler(BusinessException.class)
    public ReturnResult businessException(BusinessException e) {
        return ReturnResult.builder().code(e.getCode()).data(e.getData()).msg(e.getMsg()).build();
    }


    @ExceptionHandler(RuntimeException.class)
    public ReturnResult runtimeException(RuntimeException e) {
        return ReturnResult.builder().code(3024).data(null).msg("暂无内容").build();
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ReturnResult noHandlerFoundException(NoHandlerFoundException e) {
        return ReturnResult.builder().code(404).data(null).msg("暂无页面").build();
    }

    @ExceptionHandler(NullPointerException.class)
    public ReturnResult nullPointerException(NullPointerException e) {
        return ReturnResult.builder().code(3024).data(null).msg(e.getMessage()).build();
    }


    @ExceptionHandler(TestException.class)
    public ReturnResult testException(TestException e) {
        return ReturnResult.builder().code(3024).data(null).msg(e.getMessage()).build();
    }
}
